import axios from 'axios';
import { ElMessage } from 'element-plus';
import qs from 'qs';
const prodURL = 'https://zcp-api.yilidakeji.com/api/'; //TODO:待定
const baseURL = process.env.NODE_ENV === 'development' ? prodURL : prodURL;
import useUserStore from '@/store/modules/user'

const $http = axios.create({
  baseURL,
  transformRequest: [request => qs.stringify(request)],
  transformResponse: [responseData => {
    let formattedData = JSON.parse(responseData);
    console.log(formattedData, 'formattedDataformattedDataformattedData')
    let backendResultCode = Number(formattedData.code);
    if (backendResultCode === 1) {
      // 接口正常返回
      return {
        normal: true,
        ...formattedData.data
      }
    }else if(backendResultCode === 1000)  { 
      useUserStore().logout()
    } else {
      // alert('AXIOS' + JSON.stringify(formattedData));
      // ElMessage.fail(formattedData.message);
      ElMessage({
        message: formattedData.msg,
        type: 'warning',
      })
      return formattedData
    }
  }],
  headers: {
    token: localStorage.getItem('token') || "",
  },
  // timeout: 20000
}
);

export default {
  api: $http,
  _prodURL: prodURL,
}